package jgl.vajra.operator.part.mine.fragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.MyCarListBean;
import jgl.vajra.operator.listener.MessageDlialogListener;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.FragmentManager;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.mine.contract.MyCarListContract;
import jgl.vajra.operator.part.mine.viewModel.MyCarListViewModel;
import jgl.vajra.operator.widget.base.BaseListFragment;
import jgl.vajra.operator.widget.dialogfragment.MessageDialogFragment;
import jgl.vajra.operator.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.operator.widget.utils.ToastUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(MyCarListViewModel.class)
public class MyCarListFragment extends BaseListFragment<MyCarListViewModel, String> implements MyCarListContract.View {

    private MessageDialogFragment messageDialogDeleteCar;
    private String authorization;
    private int type;

    @Override
    protected RefreshRecyclerNetConfig getRefreshRecyclerNetConfig() {
        return new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", authorization);
                return mViewModel.getMyCarList(map);
            }
        };
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_my_car;
    }

    @Override
    public void onItemClick(int position, String itemData) {

    }

    @Override
    public void initView() {
        super.initView();
        type = getArguments().getInt("type");
        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;

       mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<List<MyCarListBean>>() {
            @Override
            public void onSucceed(List<MyCarListBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(false, false);

                mBinding.recyclerView.requestNetObjectSuccess(data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();

        mBinding.recyclerView.setRecyclerViewBackgroundRes(R.color.gray_f7);
        mBinding.recyclerView.setRootViewBackgroundRes(R.color.gray_f7);
    }
    //删除车
    @Override
    public void onDeleteCar(int position, MyCarListBean itemData) {
        if (messageDialogDeleteCar == null) {
            messageDialogDeleteCar = FragmentManager.getMessageDialogDeleteCar(new MessageDlialogListener() {
                @Override
                public void onRightClick() {
                    Map<String,Object> map=new HashMap();
                    map.put("Authorization", authorization);
                    map.put("fileId", itemData.fileId);
                    mViewModel.deleteDriverCertificate(map);
                }

                @Override
                public void onLeftClick() {
                    ToastUtils.showShort("再想想");
                }
            });
        }
        messageDialogDeleteCar.show(mActivity.getSupportFragmentManager());
    }

    //详情
    @Override
    public void onDetails(MyCarListBean itemData) {
        IntentManager.toCarDetailsActivity(mActivity,itemData.remark);
    }

    @Override
    public void returnDeleteDriverCertificate(String data) {
        ToastUtils.showShort(data);
        mBinding.recyclerView.firstLoad();
    }


    @Override
    public void onResume() {
        super.onResume();
        mBinding.recyclerView.firstLoad();
    }
}
